package cn.iocoder.yudao.module.handguard.dal.mysql.order;

import java.math.BigDecimal;
import java.util.*;

import cn.hutool.db.sql.Order;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.handguard.controller.app.orders.vo.OrdersResVo;
import cn.iocoder.yudao.module.handguard.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.handguard.tool.MybatisParameterUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.handguard.controller.admin.order.vo.*;
import org.apache.ibatis.annotations.Param;

/**
 * 订单 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface OrderMapper extends BaseMapperX<OrderDO> {

    default PageResult<OrderDO> selectPage(OrderPageReqVO reqVO) {
        //调用工具类重新拼装in条件               （条件wrapper，            get方法的方法引用，    参数list）
        LambdaQueryWrapperX <OrderDO> queryWrapper = new LambdaQueryWrapperX<OrderDO>()
                .likeIfPresent(OrderDO::getOrderNo, reqVO.getOrderNo())
                .eqIfPresent(OrderDO::getUserId, reqVO.getUserId())
//                .inIfPresent(OrderDO::getUserId, reqVO.getIds())
                .betweenIfPresent(OrderDO::getApplyTime, reqVO.getApplyTime())
                .eqIfPresent(OrderDO::getApplyUser, reqVO.getApplyUser())
                .eqIfPresent(OrderDO::getAmount, reqVO.getAmount())
                .eqIfPresent(OrderDO::getPracticalAmount, reqVO.getPracticalAmount())
                .eqIfPresent(OrderDO::getOrderState, reqVO.getOrderState())
                .eqIfPresent(OrderDO::getStates, reqVO.getStates())
                .eqIfPresent(OrderDO::getWhether, reqVO.getWhether())
                .eqIfPresent(OrderDO::getProductId, reqVO.getProductId())
                .eqIfPresent(OrderDO::getAgentId, reqVO.getAgentId())
                .eqIfPresent(OrderDO::getEquityUserId, reqVO.getEquityUserId())
                .betweenIfPresent(OrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(OrderDO::getId);

        if (reqVO.getIds()!=null && reqVO.getIds().size()>0){
            try {
                MybatisParameterUtils.cutInParameter(queryWrapper,OrderDO::getUserId, reqVO.getIds());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return selectPage(reqVO, queryWrapper);
    }

    default List<OrderDO> selectList(OrderExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<OrderDO>()
                .likeIfPresent(OrderDO::getOrderNo, reqVO.getOrderNo())
                .eqIfPresent(OrderDO::getUserId, reqVO.getUserId())
                .betweenIfPresent(OrderDO::getApplyTime, reqVO.getApplyTime())
                .eqIfPresent(OrderDO::getApplyUser, reqVO.getApplyUser())
                .eqIfPresent(OrderDO::getAmount, reqVO.getAmount())
                .eqIfPresent(OrderDO::getPracticalAmount, reqVO.getPracticalAmount())
                .eqIfPresent(OrderDO::getOrderState, reqVO.getOrderState())
                .eqIfPresent(OrderDO::getStates, reqVO.getStates())
                .eqIfPresent(OrderDO::getWhether, reqVO.getWhether())
                .eqIfPresent(OrderDO::getProductId, reqVO.getProductId())
                .eqIfPresent(OrderDO::getAgentId, reqVO.getAgentId())
                .eqIfPresent(OrderDO::getEquityUserId, reqVO.getEquityUserId())
                .betweenIfPresent(OrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(OrderDO::getId));
    }

    MemberUserBaseVO getUserInfoById(@Param("userId") Long userId);

    int getMonthCjNumber(@Param("orderNo") Long orderNo);

    BigDecimal getMonthCjMoney(@Param("orderNo") Long orderNo);

    List<OrdersResVo> getOrderListByUserList(@Param("userId")Long userId, @Param("status")Integer status);
}
